package api

import (
	"anban/service"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego"
)

type BaseController struct {
	beego.Controller
}

func (c *BaseController) Prepare() {
	c.EnableXSRF = false

	var res *utils.ResJsonStruct
	noLogin := map[string]string{
		"UserControllerMiniProgramLogin": "1",
		"UserControllerCheckLogin":       "1",
	}

	ctl, act := c.GetControllerAndAction()
	ctlAct := fmt.Sprintf("%s%s", ctl, act)

	if _, ok := noLogin[ctlAct]; !ok {
		userId := c.GetSession("miniUserId")

		if userId == nil {
			res = utils.ResJson(1000, "请先登陆", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}

		p := map[string]interface{}{}
		p["id"] = userId
		user := service.GetUserInfo(p)

		if user.Id <= 0 {
			res = utils.ResJson(1000, "请先登陆", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
}
